﻿using HuaWei.CommClass;
using HuaWei.Common;
using HuaWei.DAI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HuaWei.Report
{
    public partial class frmReportDesign : Form
    {
        public frmReportDesign(string reportCode, string path, int qczt)
        {
            DevExpress.XtraReports.UI.XtraReport report = new DevExpress.XtraReports.UI.XtraReport();
            report.LoadLayout(path);
            report.DataSource = GetReportData(reportCode, GParams.QCInfo.QCID, qczt);
            report.ShowDesignerDialog();
            report.Dispose();
        }

        private DataSet GetReportData(string reportCode, string qCID, int qczt)
        {
            IDataAccess idataAccess = DBHelp.IDA;
            DataSet dataSet = new DataSet();
            string exeStr = string.Format("select  * from XMJBXX where id='{0}'", GParams._XMID);
            dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "XMJBXX"));
            exeStr = string.Format("select  * from ZZJGXX where id='{0}'", GParams._BDID);
            dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "ZZJGXX"));
            exeStr = string.Format("select aa.BDMC,bb.JLMC,aa.BGHTBH from\r\n(select  1 as F,HTDWMC as BDMC,HTBH as BGHTBH from ZZJGXXEX where id='{0}') aa left join \r\n(select 1 as F,HTDWMC as JLMC from ZZJGXXEX where id in (select pid from ZZJGXX where id='{0}')) bb on aa.f=bb.f", GParams._BDID);
            dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "RepXX"));
            exeStr = string.Format("select  * from XMQC where bdid='{0}' and qcid={1}", GParams._BDID, qCID);
            dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "XMQC"));
            if (reportCode == "REP21")
            {
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH10, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF10"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH11, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF11"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH12, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF12"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH13, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF13"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH14, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF14"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH15, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF15"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH16, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF16"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH17, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF17"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH18, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF18"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH19, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF19"));
                if (dataSet.Tables["REP_ZQJKZF19"].Rows.Count == 1 && dataSet.Tables["REP_ZQJKZF19"].Rows[0]["hj"] != DBNull.Value)
                {
                    dataSet.Tables["REP_ZQJKZF19"].Rows[0]["LargeNum"] = RMBCapitalization.smethod(CHelp.ConvertDecimal(dataSet.Tables["REP_ZQJKZF19"].Rows[0]["hj"], 0, 0));
                }
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH20, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF20"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH21, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF21"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH22, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF22"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH31, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF2_1"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH32, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF2_2"));
                exeStr = CHelp.smethod(string.Format(CRepZFJKSQL.SEARCH33, GParams._BDID, qCID));
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQJKZF2_3"));
            }
            else if (reportCode == "REP03")
            {
                exeStr = string.Format(CRepSQL.REP03, GParams._BDID, qCID);
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQZFZS"));

                exeStr = string.Format(CRepSQL.REP03, GParams._BDID, (Convert.ToInt32(qCID) - 1).ToString());
                DataSet ds = idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_ZQZFZS");
                foreach (DataRow dr in dataSet.Tables["REP_ZQZFZS"].Rows)
                {
                    dr["zjlx"] = dr["QDBH"].ToString().Substring(0, 1);
                    DataRow[] drs = ds.Tables["REP_ZQZFZS"].Select("QDBH='" + dr["QDBH"] + "'");
                    if (drs != null && drs.Length == 1 && drs[0]["bqmjlje"] != DBNull.Value)
                    {
                        dr["qdbh"] = "403-1-a-12";
                        dr["sqmjlje"] = drs[0]["bqmjlje"];
                        dr["bqjlje"] = 11112222.333;//Convert.ToInt32(dr["bqmjlje"]) - Convert.ToInt32(dr["sqmjlje"]);
                    }
                }
            }
            else if (reportCode == "REP04")
            {
                exeStr = string.Format(CRepSQL.REP04, GParams._BDID, qCID);
                DataSet dsResult = idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_GCBG");

                exeStr = string.Format(CRepSQL.REP04, GParams._BDID, (Convert.ToInt32(qCID) - 1).ToString());
                DataSet ds = idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "REP_GCBG");
                foreach (DataRow dr in dsResult.Tables["REP_GCBG"].Rows)
                {
                    string[] qdbhArr = dr["QDBH"].ToString().Split('-');
                    dr["SubQDBH1"] = qdbhArr[0];
                    dr["SubQDBHLen1"] = qdbhArr[0].Length;
                    if (qdbhArr.Length > 1)
                    {
                        dr["SubQDBH2"] = qdbhArr[1];
                        dr["SubQDBHLen2"] = qdbhArr[1].Length;
                        if (qdbhArr.Length > 2)
                        {
                            dr["SubQDBH3"] = qdbhArr[2];
                            dr["SubQDBHLen3"] = qdbhArr[2].Length;
                            if (qdbhArr.Length > 3)
                            {
                                dr["SubQDBH4"] = qdbhArr[3];
                                dr["SubQDBHLen4"] = qdbhArr[3].Length;
                                if (qdbhArr.Length > 4)
                                {
                                    dr["SubQDBH5"] = qdbhArr[4];
                                    dr["SubQDBHLen5"] = qdbhArr[4].Length;
                                }
                            }
                        }
                    }

                    DataRow[] drs = ds.Tables["REP_GCBG"].Select("QDBH='" + dr["QDBH"] + "'");
                    if (drs != null && drs.Length == 1 && drs[0]["bqmjlje"] != DBNull.Value)
                    {
                        dr["sqmjl"] = drs[0]["bqmjl"];
                        dr["sqmjlje"] = drs[0]["bqmjlje"];
                        if (dr["bqmjlje"] != DBNull.Value)
                        {
                            dr["BQJL"] = Convert.ToInt32(dr["bqmjl"]) - Convert.ToInt32(dr["sqmjl"]);
                            dr["bqjlje"] = Convert.ToInt32(dr["bqmjlje"]) - Convert.ToInt32(dr["sqmjlje"]);
                        }
                    }
                    else
                    {
                        dr["sqmjl"] = DBNull.Value;
                        dr["sqmjlje"] = DBNull.Value;
                        dr["BQJL"] = DBNull.Value;
                        dr["bqjlje"] = DBNull.Value;
                    }
                }

                DataTable dtResult = dsResult.Tables["REP_GCBG"].Clone();
                DataRow[] drsOrder = dsResult.Tables["REP_GCBG"].Select("1=1", "bglh,SubQDBHLen1,SubQDBH1,SubQDBHLen2,SubQDBH2,SubQDBHLen3,SubQDBH3,SubQDBHLen4,SubQDBH4,SubQDBHLen5,SubQDBH5");
                foreach (DataRow dr in drsOrder)
                {
                    dtResult.ImportRow(dr);
                }
                dataSet.Tables.Add(dtResult);               
            }
            else if (reportCode == "REP11")
            {
                exeStr = string.Format(CRepSQL.REP11_1, GParams._BDID, qCID);
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "QDJL_M"));
                exeStr = string.Format(CRepSQL.REP11_2, GParams._BDID, qCID);
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "QDJL_JSS"));
                DataTable table = dataSet.Tables["QDJL_M"];
                DataTable table2 = dataSet.Tables["QDJL_JSS"];
                foreach (DataRow row in table.Rows)
                {
                    string str3 = "";
                    int a = 0;
                    table2.DefaultView.RowFilter = "pid='" + row["id"].ToString() + "'";
                    foreach (DataRowView view in table2.DefaultView)
                    {
                        a++;
                        DataRow row2 = view.Row;
                        if (!str3.Equals(""))
                        {
                            int result = 0;
                            Math.DivRem(a, 3, out result);
                            if (result.Equals(1))
                            {
                                str3 = str3 + "\n";
                            }
                        }
                        string str4 = str3;
                        str3 = str4 + "\r\n     " + row2["BDS"].ToString() + " = " + row2["JSS"].ToString() + " = " + row2["JSJG"].ToString() + "        ";
                    }
                    if (!str3.Equals(""))
                    {
                        row["jss"] = str3;
                    }
                }
                if (!(!GParams._JGLX.Equals(JGLX.CBR) || qczt.Equals(1)))
                {
                    exeStr = CRepSQL.REP11_31;
                }
                else
                {
                    exeStr = CRepSQL.REP11_30;
                }
                exeStr = string.Format(exeStr, GParams._BDID, qCID);
                dataSet.Merge(idataAccess.ExecuteDataSet(exeStr, CommandType.Text, "QDJL_MX"));
                DataColumn parentColumn = dataSet.Tables["QDJL_M"].Columns["id"];
                DataColumn childColumn = dataSet.Tables["QDJL_MX"].Columns["pid"];
                DataRelation relation = new DataRelation("QDJL_M_QDJL_MX", parentColumn, childColumn);
                dataSet.Relations.Add(relation);
            }
            return dataSet;
        }
    }
}
